package com.appspheregroup.jj;

import com.appspheregroup.jj.activity.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public abstract class ChatujakMarketActivity extends Activity {
public static final int DIALOG_CONNECTION_ERROR = 1;
	
	protected ProgressDialog progressBar;
	protected int screenWidth, screenHeight;
	
	abstract protected void initLayout();
	abstract protected void initValue();
	abstract protected void cleanUp();
	
	protected void initActivity(){
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		screenWidth = dm.widthPixels;
		screenHeight = dm.heightPixels;
		dm = null;

		initLayout();
		initValue();
		bindView();
	}
	public void bindView(){
		Button btn_home = (Button) findViewById(R.id.btn_home);
		Button btn_rec = (Button) findViewById(R.id.btn_rec);
		Button btn_usefulinfo = (Button) findViewById(R.id.btn_usefulinfo);
		Button btn_where = (Button) findViewById(R.id.btn_whereamI);
		Button btn_fav = (Button) findViewById(R.id.btn_fav);		
		
		////
		btn_home.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
		});
		btn_rec.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
		});
		btn_usefulinfo.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
		});
		btn_where.setOnClickListener(new View.OnClickListener() {
	
			@Override
			public void onClick(View v) {
		// TODO Auto-generated method stub
		
			}
		});
		btn_fav.setOnClickListener(new View.OnClickListener() {
	
		@Override
		public void onClick(View v) {
		// TODO Auto-generated method stub
		
		}
		});
		
	}
	
	public void showToast(final String text, final int duration){
		runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), text, duration).show();
			}
		});
	}
	
	protected Dialog onCreateDialog(int id) {
	    Dialog dialog;
	    switch(id) {
	    case DIALOG_CONNECTION_ERROR:

	    	AlertDialog.Builder builder = new AlertDialog.Builder(this)
			.setMessage(this.getString(R.string.alert_connection_error))
	        .setCancelable(false)
	        .setNeutralButton("OK", new DialogInterface.OnClickListener() {
	           public void onClick(DialogInterface dialog, int id) {
	                dialog.cancel();
	           }
	       });
	    	
	    	return builder.create();
	    default:
	        dialog = null;
	    }
	    
	    return dialog;
	}
	
	public ProgressDialog showProgressDialog(final String header, final String body,
			final boolean cancelable) {
		
//		dismissProgressDialog();
		
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub

				try {		
					progressBar = ProgressDialog.show(ChatujakMarketActivity.this, header, body, true, cancelable);
				} catch (Exception e) {
					// TODO: handle exception
//					e.printStackTrace();
				}
			}
		});
		return progressBar;
	}
	
	public void setProgressDialogMsg(final String msg){
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub

				try {		
					progressBar.setMessage(msg);
				} catch (Exception e) {
					// TODO: handle exception
//					e.printStackTrace();
				}
			}
		});
	}
	
	public void dismissProgressDialog() {
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					if (progressBar != null && progressBar.isShowing()){
						progressBar.dismiss();
						progressBar = null;
					}
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		});
	}
	
	public boolean isShowProgressDialog(){
		if(progressBar != null){
			return progressBar.isShowing();
		}else{
			return false;
		}
	}
	
	public boolean isOnline(){
		try{
			final ConnectivityManager connMgr = (ConnectivityManager)
		    getSystemService(Context.CONNECTIVITY_SERVICE);
		 
		    final android.net.NetworkInfo wifi =
		    connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		     
		    final android.net.NetworkInfo wimax =
		    connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIMAX);
		 
		    final android.net.NetworkInfo mobile =
		    connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		 
		    if( wifi != null && wifi.isConnected() ){
//		    	 Toast.makeText(context, "Wifi" , Toast.LENGTH_LONG).show();
		    }else if( mobile != null && mobile.isConnected() ){
//		    	 Toast.makeText(context, "Mobile 3G " , Toast.LENGTH_LONG).show();
		    }else if(wimax != null && wimax.isConnected()){
//		    	 Toast.makeText(context, "Wimax " , Toast.LENGTH_LONG).show();
		    }else{
		    	runOnUiThread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						showDialog(DIALOG_CONNECTION_ERROR);
//						showToast(getString(R.string.alert_connection_error), Toast.LENGTH_SHORT);
						dismissProgressDialog();		
					}
				});
				return false;
		    }
		}catch (Exception e) {
			e.printStackTrace();
		}
	     
     
     return true;
    }
	
	
	public static void disableKeyboard(Context context, EditText editText){
		if(editText != null){
			InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
		}
	}
}